
** set path
// gl path ""

** open log
cap log close
log using "$path/table7.log", replace

**** Legislative analysis

use "$path/foreclosures_leg_analysis.dta", clear

***** Table on legislative effects

foreach j in sen cong {

	bootstrap _b, rep(1e3) seed(94305) cluster(fips) force: reghdfe `j'_vs foreclosure_rate foreclosure_inc [aw=pop_y], a(fips sy) cluster(fips)
	local `j'b1 = _b[foreclosure_rate]
	local `j'se1 = _se[foreclosure_rate]
	local `j'bint1 = _b[foreclosure_inc]
	local `j'seint1 = _se[foreclosure_inc]
	local `j'n1 = e(N)
	local `j'nclust1 = e(N_clust)
	
	bootstrap _b, rep(1e3) seed(94305) cluster(fips) force: reghdfe `j'_vs foreclosure_rate foreclosure_inc [aw=pop_y], a(fips popyear) cluster(fips)
	local `j'b2 = _b[foreclosure_rate]
	local `j'se2 = _se[foreclosure_rate]
	local `j'bint2 = _b[foreclosure_inc]
	local `j'seint2 = _se[foreclosure_inc]
	local `j'n2 = e(N)
	local `j'nclust2 = e(N_clust)
	
	bootstrap _b, rep(1e3) seed(94305) cluster(fips) force: reghdfe `j'_vs foreclosure_rate foreclosure_inc [aw=pop_y], a(i.fips##c.year sy) cluster(fips)
	local `j'b3 = _b[foreclosure_rate]
	local `j'se3 = _se[foreclosure_rate]
	local `j'bint3 = _b[foreclosure_inc]
	local `j'seint3 = _se[foreclosure_inc]
	local `j'n3 = e(N)
	local `j'nclust3 = e(N_clust)
	
	bootstrap _b, rep(1e3) seed(94305) cluster(fips) force: reghdfe `j'_vs foreclosure_rate foreclosure_inc [aw=pop_y], a(i.fips##c.year popyear) cluster(fips)
	local `j'b4 = _b[foreclosure_rate]
	local `j'se4 = _se[foreclosure_rate]
	local `j'bint4 = _b[foreclosure_inc]
	local `j'seint4 = _se[foreclosure_inc]
	local `j'n4 = e(N)
	local `j'nclust4 = e(N_clust)

}

log close

quietly {
	cap log close
	set linesize 255
	log using "$path/foreclosure_leg.tex", text replace
	
	noisily dis "\begin{table}[h]"
	noisily dis "\centering"
	noisily dis "\caption{\textbf{Effects of Housing Foreclosures on Legislative Elections, County Level, 2002--2016.}"
	noisily dis "\label{tab:foreclosures_leg}}"
	noisily dis "\begin{tabular}{lcccc}"
	noisily dis "\toprule \toprule"
	noisily dis " & \multicolumn{4}{c}{Dem Senate Vote Pct (0-100)}\\"
	noisily dis "\midrule"
	noisily dis "Foreclosures Per 1,000 People & " %4.2f `senb1' " & " %4.2f `senb2' " & " %4.2f `senb3' " & " %4.2f `senb4'  "\\"
	noisily dis " & (" %4.2f `sense1' ") & (" %4.2f `sense2' ") & (" %4.2f `sense3' ") & (" %4.2f `sense4' ") \medskip \\"
	noisily dis "Foreclosures $ \times $ Inc Party & " %4.2f `senbint1' " & " %4.2f `senbint2' " & " %4.2f `senbint3' " & " %4.2f `senbint4' "\\"
	noisily dis " & (" %4.2f `senseint1' ") & (" %4.2f `senseint2' ") & (" %4.2f `senseint3' ") & (" %4.2f `senseint4' ") \medskip \\"
	noisily dis " N & " %4.0f `senn1' " & " %4.0f `senn2' " & " %4.0f `senn3' " & " %4.0f `senn4'  " \\ "
	noisily dis " \# Counties & " %4.0f `sennclust1' " & " %4.0f `sennclust2' " & " %4.0f `sennclust3' " & " %4.0f `sennclust4' " \\ "
	noisily dis "\midrule"
	noisily dis " & \multicolumn{4}{c}{Dem House Vote Pct (0-100)}\\"
	noisily dis "\midrule"
	noisily dis "Foreclosures Per 1,000 People & " %4.2f `congb1' " & " %4.2f `congb2' " & " %4.2f `congb3' " & " %4.2f `congb4'  "\\"
	noisily dis " & (" %4.2f `congse1' ") & (" %4.2f `congse2' ") & (" %4.2f `congse3' ") & (" %4.2f `congse4' ")  \medskip \\"
	noisily dis "Foreclosures $ \times $ Inc Party & " %4.2f `congbint1' " & " %4.2f `congbint2' " & " %4.2f `congbint3' " & " %4.2f `congbint4' "\\"
	noisily dis " & (" %4.2f `congseint1' ") & (" %4.2f `congseint2' ") & (" %4.2f `congseint3' ") & (" %4.2f `congseint4' ")  \medskip \\"

	noisily dis " N & " %8.0fc `congn1' " & " %8.0fc `congn2' " & " %8.0fc `congn3' " & " %8.0fc `congn4' " \\ "
	noisily dis " \# Counties & " %4.0fc `congnclust1' " & " %4.0fc `congnclust2' " & " %4.0fc `congnclust3' " & " %4.0fc `congnclust4' " \\ "
	
	noisily dis "\midrule"
	
	noisily dis "County Fixed Effects & Y & Y & Y & Y  \\"
	noisily dis "State-Year Fixed Effects & Y & N & Y & N \\"
	noisily dis "Pop Decile-Year Fixed Effects & N & Y & N & Y \\"
	noisily dis "County Linear Trends & N & N & Y & Y \\"
	noisily dis "Population Weights & Y & Y & Y & Y \\"
	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{5}{p{.65\textwidth}}{\footnotesize Standard errors generated from 1,000 iterations of a county-level block bootstrap procedure.  "
	noisily dis "Inc Party is 1 for Dem, -1 for Rep.  Main effect for Inc Party is absorbed by fixed effects.}"
	noisily dis "\end{tabular}"
	noisily dis "\end{table}"
	
	log off
	
}
